from concurrent.futures import ThreadPoolExecutor, as_completed
from threading import Lock
import time


def get_html(times):
    time.sleep(times)
    print(f"get page {times}")
    return times


executor = ThreadPoolExecutor(max_workers=2)

# # 提交到线程池，立即返回，非阻塞
# t1 = executor.submit(get_html, 3)
# t2 = executor.submit(get_html, 2)

# print(t1.done())  # 判断t1是否完成
# time.sleep(4)
# print(t1.done())
# print(t1.result())
urls = [3, 2, 4]
all_task = [executor.submit(get_html, url) for url in urls]

for future in as_completed(all_task):
    data = future.result()
    print(f"get page {data} success")


# 值是顺序返回的
# for data in executor.map(get_html, urls):
#     print(f"get page {data} success")
